package LeetCode.month02.day33;

import java.util.Arrays;

public class No3411 {
    public int maxLength(int[] nums) {
        int mx = Arrays.stream(nums).max().getAsInt();
        int alLcm = 1;
        for (int num : nums) {
            alLcm = getLCM(alLcm,num);
        }
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            int m = 1;
            int l = 1;
            int g = 0;
            for (int j = i; j < nums.length && m <= alLcm * mx; j++) {
                int x = nums[j];
                m*=x;
                l = getLCM(l,x);
                g = getGCD(g,x);
                if(m==l*g){
                    ans =Math.max(ans,j-i+1);
                }
            }
        }
        return ans;
    }

    public static int getGCD(int a,int b){
        while(a!=0){
            int temp = a;
            a = b%a;
            b = temp;
        }
        return b;
    }

    public static int getLCM(int a,int b){
        return a*b/getGCD(a,b);
    }
}
